﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.DBModule;
using ZX.BLL.Extend;
using ZX.EHR.Library;
using ZX.CRM.Bussiness;
using ZX.SYS.Library;
using ZX.CRM.Library;
using ZX.BLL;
using ZX.Common;
using ZX.Web.UI.Controls.ControlBind;

namespace ZX.WebApp.Crm.Customer
{
    public partial class AddCustomer : EditUIPage
    {
        BTCrmCustomer bll = new BTCrmCustomer();
        BDatPhraseDetail bllDetail = new BDatPhraseDetail();
        string strLinkMan = "", strLinkManRecord = "",
               strAttach = "", strOrder = "", strPay = "",
               strTeachSump = "", strCustomer = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            string strEntity = this.GetStringFromQueryString("entity", "E_Menu");
            string ids = this.GetStringFromQueryString("id", "");
            if (DataAction == "update")
            {
                ids = "00000000-0000-0000-0000-000000000000";
            }
            BaseEntity instance = ZX.Common.Globals.getEntity(strEntity) as BaseEntity;
            BaseBussnies bInstance = new BLL.Extend.BaseBussnies(instance);
            BaseEntity entity = bInstance.GetEntityById(ids) as BaseEntity;
            string str = CreateInstanceJsObject(instance, entity);
            this.EditTemplate.Text = str.Replace("@@@@","");

            //if (!this.IsPostBack)
            {
                #region-------
                //省份
                IList<SysProvince> ListProvince = bll.GetAllProvince();
                DropDownListControl.DropDataSourceBind(this.Province, ListProvince, "Name", "Code");
                //虚拟部门
                //IList<DatDepartment> ListDept = bll.GetVirtualDepartment(this.UserId);  //(Old)
                //IList<V_CrmUserArea> ListDept = bll.GetVirtualDepartmentNew(this.UserId);
                //DropDownListControl.DropDataSourceBind(this.AreaId, ListDept, "DeptName", "DeptID");
                //客户规模(员工人数)
                IList<DatPhraseDetail> ListDetailNumber = bllDetail.GetDatPhraseDetailByClassValue("CustomerScope");
                DropDownListControl.DropDataSourceBind(this.PersonId, ListDetailNumber, "PhraseText", "PhraseID");
                //行业
                //IList<DatPhraseDetail> ListDetailTrade = bllDetail.GetDatPhraseDetailByClassValue("Trade");
                //DropDownListControl.DropDataSourceBind(this.TradeId, ListDetailTrade, "PhraseText", "PhraseID");
                //客户类型
                IList<DatPhraseDetail> ListDetailType = bllDetail.GetDatPhraseDetailByClassValue("CustomerType");
                DropDownListControl.DropDataSourceBind(this.TypeId, ListDetailType, "PhraseText", "PhraseID");
                //客户级别
                IList<DatPhraseDetail> ListDetailLevel = bllDetail.GetDatPhraseDetailByClassValue("CustomerLevel");
                DropDownListControl.DropDataSourceBind(this.LevelId, ListDetailLevel, "PhraseText", "PhraseID");
                //产值
                IList<DatPhraseDetail> ListDetailProduct = bllDetail.GetDatPhraseDetailByClassValue("Product");
                DropDownListControl.DropDataSourceBind(this.ProductId, ListDetailProduct, "PhraseText", "PhraseID");
                //客户来源
                IList<DatPhraseDetail> ListDetailOrigin = bllDetail.GetDatPhraseDetailByClassValue("CustomerSource");
                DropDownListControl.DropDataSourceBind(this.OriginId, ListDetailOrigin, "PhraseText", "PhraseID");
                //公司性质
                IList<DatPhraseDetail> ListDetailProperty = bllDetail.GetDatPhraseDetailByClassValue("Property");
                DropDownListControl.DropDataSourceBind(this.PropertyId, ListDetailProperty, "PhraseText", "PhraseID");

                #endregion

                #region-------
                //新增信息
                if (DataAction == "insert")
                {
                    this.ID.Value = Guid.NewGuid().ToString();
                    this.Code.Value = IDMaker.GetSequenceStr("CUS001");
                }
                else  //编辑信息
                { 
                    string id = this.GetStringFromQueryString("id", "");
                    if (id != null)
                    {
                        if (strEntity == "CrmCustomer")
                        {
                            #region --------- Set Value CrmCustomer ---------
                            IList<V_CrmCustomer> list = bll.GetAllCustomerInfo(new Guid(id));
                            if (list != null && list.Count > 0)
                            {
                                CustomerDataBind(list);
                            }
                            #endregion
                        }
                        #region ---------- jsonData ----------
                        //联系人
                        IList<V_CrmLinkMan> listLinkMan = bll.GetLinkManInfoByCustomerId(new Guid(id));
                        if (listLinkMan != null && listLinkMan.Count > 0)
                        {
                            IList<BaseEntity> _listLinkMan = new List<BaseEntity>();
                            foreach (var _e in listLinkMan)
                            {
                                _listLinkMan.Add(_e);
                            }
                            strLinkMan = _listLinkMan.ToJSON().Replace("\n", "").Replace("\r","").Replace("@@", "");
                        }
                        //行销中心记录
                        IList<V_CrmActivityTel> listLinkRecord = bll.GetLinkRecordInfoByCustomerId(new Guid(id));
                        if (listLinkRecord != null && listLinkRecord.Count > 0)
                        {
                            IList<BaseEntity> _listLinkRecord = new List<BaseEntity>();
                            foreach (var _e in listLinkRecord)
                            {
                                _listLinkRecord.Add(_e);
                            }
                            strLinkManRecord = _listLinkRecord.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                        }
                        //附件
                        IList<V_CrmCustomerAttach> listAttach = bll.GetAllCrmCustomerAttachInfo(new Guid(id));
                        if (listAttach != null && listAttach.Count > 0)
                        {
                            IList<BaseEntity> _listAttach = new List<BaseEntity>();
                            foreach (var _e in listAttach)
                            {
                                _listAttach.Add(_e);
                            }
                            strAttach = _listAttach.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                        }
                        //订单
                        IList<V_CrmCustomerInfoOrder> listCustomerOrder = bll.GetAllOrderInfos(new Guid(id));
                        if (listCustomerOrder != null && listCustomerOrder.Count > 0)
                        {
                            IList<BaseEntity> _listCustomerOrder = new List<BaseEntity>();
                            foreach (var _e in listCustomerOrder)
                            {
                                _listCustomerOrder.Add(_e);
                            }
                            strOrder = _listCustomerOrder.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                        }
                        //缴款单
                        IList<V_CrmPay> listCusoterPay = bll.GetAllPayInfo(new Guid(id));
                        if (listCusoterPay != null && listCusoterPay.Count > 0)
                        {
                            IList<BaseEntity> _listCusoterPay = new List<BaseEntity>();
                            foreach (var _e in listCusoterPay)
                            {
                                _listCusoterPay.Add(_e);
                            }
                            strPay = _listCusoterPay.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                        }
                        //讲师总结
                        IList<V_CrmTeachSumup> listTeachSumup = bll.GetAllTeachSumup(new Guid(id));
                        if (listTeachSumup != null && listTeachSumup.Count > 0)
                        {
                            IList<BaseEntity> _listTeachSumup = new List<BaseEntity>();
                            foreach (var _e in listTeachSumup)
                            {
                                _listTeachSumup.Add(_e);
                            }
                            strTeachSump = _listTeachSumup.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                        }
                        //客户数据
                        IList<CrmCustomer> listCrmCustomer = bll.GetCrmCustomer(new Guid(id));
                        if (listCrmCustomer != null && listCrmCustomer.Count > 0)
                        {
                            IList<BaseEntity> _listCrmCustomer = new List<BaseEntity>();
                            foreach (var _e in listCrmCustomer)
                            {
                                _listCrmCustomer.Add(_e);
                            }
                            strCustomer = _listCrmCustomer.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                        }
                        #endregion
                    }
                }
                #endregion
            }
        }

        private void CustomerDataBind(IList<V_CrmCustomer> list)
        {
            this.ID.Value = list[0].ID.ToString();

            this.ModifyUser.Value = this.UserId.ToString();
            this.ModifyTime.Value = DateTime.Now.ToString();

            if (this.GetStringFromQueryString("back", "") == "2")
            {
                this.txtAddUserId.Value = this.User.UName;
                this.hiddenAddUser.Value = this.UserId.ToString();

                if (list[0].DecisionTel.ToString() == "")
                {
                    this.DecisionTel.Value = "";
                    this.hiddenDecisionTel.Value = "";
                }
                else
                {
                    this.DecisionTel.Value = list[0].DecisionTel.ToString();
                    this.hiddenDecisionTel.Value = list[0].DecisionTel.ToString();
                }
                if (list[0].DecisionTel2.ToString() == "")
                {
                    this.DecisionTel2.Value = "";
                    this.hiddenDecisionTel2.Value = "";
                }
                else
                {
                    this.DecisionTel2.Value = list[0].DecisionTel2.ToString();
                    this.hiddenDecisionTel2.Value = list[0].DecisionTel2.ToString();
                }
            }
            else
            {
                this.txtAddUserId.Value = list[0].UName.ToString(); //信息专员名称
                this.hiddenAddUser.Value = list[0].AddUser.ToString(); //信息专员ID

                if (list[0].DecisionTel.ToString() == "")
                {
                    this.DecisionTel.Value = "";
                    this.hiddenDecisionTel.Value = "";
                }
                else
                {
                    this.DecisionTel.Value = "●●●●●●●●";
                    this.hiddenDecisionTel.Value = list[0].DecisionTel.ToString();
                }
                if (list[0].DecisionTel2.ToString() == "")
                {
                    this.DecisionTel2.Value = "";
                    this.hiddenDecisionTel2.Value = "";
                }
                else
                {
                    this.DecisionTel2.Value = "●●●●●●●●";
                    this.hiddenDecisionTel2.Value = list[0].DecisionTel2.ToString();
                }
            }
            this.Code.Value = list[0].Code.ToString();
            this.CustomerName.Value = list[0].CustomerName.ToString();

            IList<V_SysProvince> ListProvince = bll.GetProvinceInfo(list[0].Province);
            if (ListProvince != null && ListProvince.Count > 0)
            {
                this.Province.SelectedIndex = this.Province.Items.IndexOf(this.Province.Items.FindByText(ListProvince[0].Province));
            }
            IList<V_SysProvince> ListCity = bll.GetProvinceInfo(list[0].Province, list[0].City);
            if (ListCity != null && ListCity.Count > 0)
            {
                //绑定城市
                IList<SysCity> city = bll.GetAllCityByProviceName(list[0].Province);
                DropDownListControl.DropDataSourceBind(this.City, city, "Name", "Code");
                this.City.SelectedIndex = this.City.Items.IndexOf(this.City.Items.FindByText(ListCity[0].City));
            }
            IList<V_SysProvince> ListArea = bll.GetProvinceInfo(list[0].Province, list[0].City);
            if (ListArea != null && ListArea.Count > 0)
            {
                //绑定区县
                this.Area.DataSource = ListArea;
                this.Area.DataTextField = "Area";
                this.Area.DataValueField = "AreaCode";
                this.Area.DataBind();

                this.Area.SelectedIndex = this.Area.Items.IndexOf(this.Area.Items.FindByText(list[0].Area));
            }
            this.Address.Value = list[0].Address.ToString();
            this.Decisionmaker.Value = list[0].Decisionmaker.ToString();
            this.DecisionPosition.Value = list[0].DecisionPosition.ToString();

            this.Fax.Value = list[0].Fax.ToString();
            this.Tel1.Value = list[0].Tel1.ToString();
            this.Tel2.Value = list[0].Tel2.ToString();
            this.DecisionEmail.Value = list[0].DecisionEmail.ToString();
            this.Email.Value = list[0].Email.ToString();
            this.CompanyNetting.Value = list[0].CompanyNetting.ToString();
            this.UnionId.Value = list[0].UnionId.ToString();
            this.CompanyCreateDate.Value = list[0].CompanyCreateDate.ToString("yyyy-MM-dd") == "1900-01-01" ? "" : list[0].CompanyCreateDate.ToString("yyyy-MM-dd");
            this.Post.Value = list[0].Post.ToString();
            this.CompanyAttention.Value = list[0].CompanyAttention.ToString();
            this.MnemonicCode.Value = list[0].MnemonicCode.ToString();
            this.CompanyProduct.Value = list[0].CompanyProduct.ToString();
            this.CusTestimony.Value = list[0].CusTestimony.ToString();
            this.ManagerQuestion.Value = list[0].ManagerQuestion.ToString();
            this.StrongPoint.Value = list[0].StrongPoint.ToString();
            this.Explain.Value = list[0].Explain.ToString();
            this.AddTime.Value = list[0].AddTime.ToString();
            this.AreaId.Value = list[0].AreaId.ToString();
            this.txtAreaId.Value = list[0].CustomerArea.ToString();
            this.TradeId.Value = list[0].TradeId.ToString();
            this.txtTradeId.Value = list[0].CustomerTrade.ToString();

            DropDownListControl.DropDownListSelect(this.DecisionSex, list[0].DecisionSex.ToString());
            DropDownListControl.DropDownListSelect(this.PersonId, list[0].PersonId.ToString());
            //DropDownListControl.DropDownListSelect(this.AreaId, list[0].AreaId.ToString());
            //DropDownListControl.DropDownListSelect(this.TradeId, list[0].TradeId.ToString());
            DropDownListControl.DropDownListSelect(this.TypeId, list[0].TypeId.ToString());
            DropDownListControl.DropDownListSelect(this.LevelId, list[0].LevelId.ToString());
            DropDownListControl.DropDownListSelect(this.ProductId, list[0].ProductId.ToString());
            DropDownListControl.DropDownListSelect(this.OriginId, list[0].OriginId.ToString());
            DropDownListControl.DropDownListSelect(this.PropertyId, list[0].PropertyId.ToString());
        }
        
        #region----
        /// <summary>
        /// 添加人ID
        /// </summary>
        public string UserID
        {
            get { return this.UserId.ToString(); }
        }
        /// <summary>
        /// 页面标题
        /// </summary>
        public string PageTitle
        {
            get { return this.GetStringFromQueryString("title", ""); }
        }
        /// <summary>
        /// 状态
        /// </summary>
        public string Flag
        {
            get { return this.GetStringFromQueryString("flag", ""); }
        }
        /// <summary>
        /// 获取类型 (0:顾问 1:经理)
        /// </summary>
        public string Type
        {
            get { return this.GetStringFromQueryString("type",""); }
        }
        /// <summary>
        /// 联系人信息
        /// </summary>
        public string LinkMan
        {
            get { return strLinkMan; }
        }
        /// <summary>
        /// 行销中心记录
        /// </summary>
        public string LinkRecord
        {
            get { return strLinkManRecord; }
        }
        /// <summary>
        /// 附件信息
        /// </summary>
        public string Attach
        {
            get { return strAttach; }
        }
        /// <summary>
        /// 订单信息
        /// </summary>
        public string Order
        {
            get { return strOrder; }
        }
        /// <summary>
        /// 缴款单信息
        /// </summary>
        public string Pay
        {
            get { return strPay; }
        }
        /// <summary>
        /// 讲师总结
        /// </summary>
        public string TeachSump
        {
            get { return strTeachSump; }
        }
        /// <summary>
        /// 客户数据
        /// </summary>
        public string Customer
        {
            get { return strCustomer; }
        }
        /// <summary>
        /// 判断是否为信息专员还是顾问 0:信息专员 1:顾问
        /// </summary>
        public string Return
        {
            get { return this.GetStringFromQueryString("back", ""); }
        }
        /// <summary>
        /// 客户退回时 在退回时修改 退回ID
        /// </summary>
        public string ReturnId
        {
            get { return this.GetStringFromQueryString("rId",""); }
        }
        #endregion
    }
}